{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4bac8749",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 1.使用Python发送纯文本邮件"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "174bd3e1",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1.1使用smtplib发送纯文本邮件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "8b7ed2da",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "#2.以QQ邮箱为例，无需安装第三方库\n",
    "key='***********'                                #换成你的QQ邮箱SMTP的授权码(QQ邮箱设置里)\n",
    "import smtplib\n",
    "\n",
    "EMAIL_ADDRESS='**********@qq.com'                     #换成你的邮箱地址\n",
    "EMAIL_PASSWORD=key\n",
    "smtp=smtplib.SMTP('smtp.qq.com',25)\n",
    "\n",
    "import ssl\n",
    "context=ssl.create_default_context()\n",
    "sender=EMAIL_ADDRESS                                         #发件邮箱\n",
    "receiver=EMAIL_ADDRESS                                       #收件邮箱\n",
    "from email.message import EmailMessage\n",
    "\n",
    "subject=\"python email subject\"\n",
    "body=\"ii Hello,this is an email sent by python!------\"\n",
    "msg=EmailMessage()\n",
    "msg['subject']=subject                                        #邮件主题\n",
    "msg['From']=sender\n",
    "msg['To']=receiver\n",
    "msg.set_content(body)                                         #邮件内容\n",
    "\n",
    "with smtplib.SMTP_SSL(\"smtp.qq.com\",465,context=context) as smtp:\n",
    "    smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)\n",
    "    smtp.send_message(msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b1b705fd",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(221, b'Bye.')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1.\n",
    "key='**********ea'\n",
    "import smtplib\n",
    "EMAIL_ADDRESS='********4@qq.com'\n",
    "EMAIL_PASSWORD=key\n",
    "smtp=smtplib.SMTP('smtp.qq.com',25)\n",
    "\n",
    "import ssl\n",
    "context=ssl.create_default_context()\n",
    "smtp=smtplib.SMTP_SSL(\"smtp.qq.com\",465,context=context)\n",
    "smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)               #smtp.login(EMAIL_ADRESS,EMAIL_PASSWORD)\n",
    "sender=EMAIL_ADDRESS\n",
    "receiver=EMAIL_ADDRESS\n",
    "\n",
    "subject=\"python email subject\"\n",
    "body=\"Hello,this is an email sent by python\"\n",
    "\n",
    "msg=f\"Subject:{subject}\\n\\n{body}\"\n",
    "smtp.sendmail(sender,receiver,msg)\n",
    "smtp.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "decb9f22",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1.2 如何使用email.message来发送HTML内容，附件"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1b8c72d",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 1.2.1附带图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b223c718",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "key='*********ea'\n",
    "import smtplib\n",
    "\n",
    "EMAIL_ADDRESS='*******@qq.com'\n",
    "EMAIL_PASSWORD=key\n",
    "smtp=smtplib.SMTP('smtp.qq.com',25)\n",
    "# smtp=smtplib.SMTP_SSL('smtp.qq.com',465)\n",
    "import ssl\n",
    "context=ssl.create_default_context()\n",
    "# smtp=smtplib.SMTP_SSL(\"smtp.qq.com\",465,context=context)\n",
    "# smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)\n",
    "sender=EMAIL_ADDRESS\n",
    "receiver=EMAIL_ADDRESS\n",
    "from email.message import EmailMessage\n",
    "\n",
    "subject=\"python email subject\"\n",
    "body=\"Hello,this is an email sent by python!\"\n",
    "msg=EmailMessage()\n",
    "msg['subject']=subject\n",
    "msg['From']=sender\n",
    "msg['To']=receiver\n",
    "msg.set_content(body)\n",
    "\n",
    "file_name='./1.png'\n",
    "with open(file_name,'rb') as f:\n",
    "    file_data=f.read()\n",
    "\n",
    "msg.add_attachment(file_data,maintype='image',subtype='png',filename=file_name)\n",
    "\n",
    "with smtplib.SMTP_SSL(\"smtp.qq.com\",465,context=context) as smtp:\n",
    "    smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)\n",
    "    smtp.send_message(msg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "550ac3c4",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 1.2.2附带html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1628d0d8",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "key='****8****8ea'\n",
    "import smtplib\n",
    "\n",
    "EMAIL_ADDRESS='********4@qq.com'\n",
    "EMAIL_PASSWORD=key\n",
    "smtp=smtplib.SMTP('smtp.qq.com',25)\n",
    "\n",
    "import ssl\n",
    "context=ssl.create_default_context()\n",
    "\n",
    "sender=EMAIL_ADDRESS\n",
    "receiver=EMAIL_ADDRESS\n",
    "from email.message import EmailMessage\n",
    "\n",
    "subject=\"python email subject\"\n",
    "body=\"Hello,this is an email sent by python!\"\n",
    "msg=EmailMessage()\n",
    "msg['subject']=subject\n",
    "msg['From']=sender\n",
    "msg['To']=receiver\n",
    "msg.set_content(body)\n",
    "\n",
    "msg.add_alternative(\n",
    "\"\"\"\\\n",
    "<!DOCTYPE html>\n",
    "<html>\n",
    "    <body>\n",
    "        <h1 style=\"color:Orange;\">This 我is an email sent by Python</h1>\n",
    "    </body>\n",
    "</html>\n",
    "\"\"\",subtype='html'\n",
    ")\n",
    "\n",
    "#msg.add_attachment(file_data,maintype='image',subtype='png',filename=file_name)\n",
    "\n",
    "with smtplib.SMTP_SSL(\"smtp.qq.com\",465,context=context) as smtp:\n",
    "    smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)\n",
    "    smtp.send_message(msg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b673379",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1.3给多个用户发邮件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1233ee8",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "key='*********ea'\n",
    "import smtplib\n",
    "\n",
    "EMAIL_ADDRESS='****8***8@qq.com'\n",
    "EMAIL_PASSWORD=key\n",
    "smtp=smtplib.SMTP('smtp.qq.com',25)\n",
    "# smtp=smtplib.SMTP_SSL('smtp.qq.com',465)\n",
    "import ssl\n",
    "context=ssl.create_default_context()\n",
    "# smtp=smtplib.SMTP_SSL(\"smtp.qq.com\",465,context=context)\n",
    "# smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)\n",
    "sender=EMAIL_ADDRESS\n",
    "receiver=EMAIL_ADDRESS\n",
    "from email.message import EmailMessage\n",
    "\n",
    "subject=\"python email subject\"\n",
    "body=\"Hello,this is an email sent by python!\"\n",
    "msg=EmailMessage()\n",
    "msg['subject']=subject\n",
    "msg['From']=sender\n",
    "msg['To']=[receiver,receiver]         #多个用户\n",
    "msg.set_content(body)\n",
    "\n",
    "file_name='./1.png'\n",
    "with open(file_name,'rb') as f:\n",
    "    file_data=f.read()\n",
    "\n",
    "#msg.add_attachment(file_data,maintype='image',subtype='png',filename=file_name)\n",
    "\n",
    "with smtplib.SMTP_SSL(\"smtp.qq.com\",465,context=context) as smtp:\n",
    "    smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)\n",
    "    smtp.send_message(msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bc1ab3f6",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "key='n******a'\n",
    "import smtplib\n",
    "\n",
    "EMAIL_ADDRESS='******4@qq.com'\n",
    "EMAIL_PASSWORD=key\n",
    "smtp=smtplib.SMTP('smtp.qq.com',25)\n",
    "\n",
    "contacts=[EMAIL_ADDRESS,EMAIL_ADDRESS,EMAIL_ADDRESS]\n",
    "\n",
    "# smtp=smtplib.SMTP_SSL('smtp.qq.com',465)\n",
    "import ssl\n",
    "context=ssl.create_default_context()\n",
    "# smtp=smtplib.SMTP_SSL(\"smtp.qq.com\",465,context=context)\n",
    "# smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)\n",
    "sender=EMAIL_ADDRESS\n",
    "\n",
    "from email.message import EmailMessage\n",
    "\n",
    "\n",
    "# file_name='./1.png'\n",
    "# with open(file_name,'rb') as f:\n",
    "#     file_data=f.read()\n",
    "\n",
    "#msg.add_attachment(file_data,maintype='image',subtype='png',filename=file_name)\n",
    "\n",
    "with smtplib.SMTP_SSL(\"smtp.qq.com\",465,context=context) as smtp:\n",
    "    smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)\n",
    "    for contact in contacts:\n",
    "        subject=\"python email subject\"\n",
    "        body=f\"Hello {contact},this is an email sent by python!\"\n",
    "        msg=EmailMessage()\n",
    "        msg['subject']=subject\n",
    "        msg['From']=sender\n",
    "        msg['To']=contact       #多个用户\n",
    "        msg.set_content(body)\n",
    "        smtp.send_message(msg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}